package com.guigu.dh.controller;


import com.guigu.dh.common.result.Result;
import com.guigu.dh.common.utils.AuthContextHolder;
import com.guigu.dh.pojo.user.Patient;
import com.guigu.dh.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    // 查询就诊人列表
    @GetMapping("/auth/list")
    public Result getListPatient(HttpServletRequest request){
        // 获取当前登录的用户id
        Long userId = AuthContextHolder.getId(request);
        // 调用方法根据用户id查询就诊人信息列表
        List<Patient> list = patientService.getPatientList(userId);
        return Result.ok(list);
    }

    // 根据就诊人ID查询
    @GetMapping("/auth/byId/{id}")
    public Result findByIdPatient(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    // 添加就诊人信息
    @PostMapping("/auth/addPatient")
    public Result addPatient(@RequestBody Patient patient,HttpServletRequest request){
        // 获取当前登录的用户id
        Long userId = AuthContextHolder.getId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    // 修改就诊人信息
    @PutMapping("/auth/updatePatient")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    // 删除就诊人信息的方法
    @DeleteMapping("/auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }
}
